package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	//http://localhost/api/album/trackInfo/uploadTrack
	@Operation(summary = "上传声音")
	@PostMapping("/uploadTrack")
	public Result uploadTrack(MultipartFile file){
		Map<String,Object> map = trackInfoService.uploadTrack(file);
		return Result.ok(map);
	}
	//http://localhost/api/album/trackInfo/saveTrackInfo
	@TsLogin
	@Operation(summary = "保存声音")
	@PostMapping("/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfo trackInfo) {
		//	获取用户Id
		Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
		//	调用服务层方法
		trackInfoService.saveTrackInfo(trackInfo, userId);
		//	返回数据
		return Result.ok();
	}
	//http://localhost/api/album/trackInfo/findUserTrackPage/1/10
	@TsLogin
	@Operation(summary = "查看声音列表")
	@PostMapping("/findUserTrackPage/{pageNo}/{pageSize}")
	public Result findUserTrackPage(@PathVariable Long pageNo,
									@PathVariable Long pageSize,
									@RequestBody TrackInfoQuery trackInfoQuery) {
		//	需要获取用户Id
		Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
		trackInfoQuery.setUserId(userId);
		//	构建分页对象
		Page<TrackListVo> page = new Page<>(pageNo, pageSize);
		//	调用服务层方法
		IPage<TrackListVo> iPage = this.trackInfoService.getUserTrackPage(page, trackInfoQuery);
		return Result.ok(iPage);
	}
}

